package ru.smc.hogwarts.filters;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SubjectsFilter implements Filter {

   public SubjectsFilter() {
    	
    }

	public void destroy() {
		
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		int user_role = -1;
		
		HttpSession session = ((HttpServletRequest) request).getSession();
		try {
			user_role = (Integer)session.getAttribute("userRole");
		} catch(NullPointerException e) {}
		
		if (user_role == 1 || user_role == 0) {
			chain.doFilter(request, response);
		} else {
			request.getRequestDispatcher("/pages/error/error.jsp").forward(request, response);
		}
	}

	public void init(FilterConfig fConfig) throws ServletException {
		
	}

}
